﻿var iPageNo = 1;
$(document).ready(function () {


    $("input[id$='txtDateBirth']").datepicker();

    $("select[id$='ddlEducation']").change(function () {
        var valEdu = $(this).val();
        var textEdu = $(this).text();
        if (valEdu === 'Other' || ) {
            $("input[id$='txtOtherEducation']").removeAttr("disabled");
        }
        else {
            $("input[id$='txtOtherEducation']").val('');
            $("input[id$='txtOtherEducation']").attr("disabled", "disabled");
            
        }
    });

    $("#btnSubmitCus").click(function () {
        var sBranch = GetValDrop("ddlBranch");
        //sBranch = $("select[id$='ddlBranch'] option:selected").val();

        var sFullName = GetValTextbox("txtFullName");
        var sNRIC = parseInt(GetValTextbox("txtNRIC"));
        var txtDateBirth = GetValTextbox("txtDateBirth");
        var sGender = parseInt(GetValDrop("ddlGender"));
        var sCity = parseInt(GetValDrop("ddlCityzen"));
        var sEducation = parseInt(GetValDrop("ddlEducation"));
        var txtOtherEdu = GetValTextbox("txtOtherEducation");




        if (ValidateForm(sBranch, sFullName, sNRIC, txtDateBirth, sGender, sCity, sEducation) === true) {

            $.ajax({
                url: "Customer.aspx/InsertCustomer",
                type: "POST",
                data: "{sBranch:'" + sBranch + "',sFullName:'" + sFullName + "',sNRIC:" + sNRIC + ",sDateBirth:'" + txtDateBirth + "', sGender:" + sGender + ",sCity:" + sCity + ",sEducation:" + sEducation + ",sOtherEdu:'" + txtOtherEdu + "'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    InsertCallBack(data);
                },
                error: function () { OnError(); }
            });

        }

    });

    GetCustomer(iPageNo);
});


function GetCustomer(currPage) {
    iPageNo = currPage;
    $.ajax({
        url: "Customer.aspx/GetCustomer",
        type: "POST",
        data: "{iPageNo:" + iPageNo + "}",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            GetCustomer_CallBack(data);
        },
        error: function () { OnError(); }
    });

}


function GetCustomer_CallBack(data) {
    var sRes = JSON.parse(data.d);
    $("#tabCus tr:gt(0)").remove();

    $("#CusTemplate").tmpl(sRes.ListCus).appendTo("#tabCus");

    if (parseInt(sRes.TotalPage) > 1) {
        $("#paging").html(GenaratePaging(sRes.TotalPage));
    }
    else
        $("#paging").text('');

}


function GenaratePaging(TotalPage) {
    var htmlPag = '';
    for (var i = 1; i <= TotalPage; i++) {
        if (iPageNo === i)
            htmlPag += "<span style='font-weight:Bold; padding-left:5px'>" + i + "</span>";
        else
            htmlPag += "<span style='cursor:pointer;padding-left:5px' onclick=GetCustomer(" + i + ")>" + i + "</span>";
    }
    
    return htmlPag;
}

function InsertCallBack(data) {
    
    if (parseInt(data.d) === 1) {
        alert('Insert customer sussessfull');
        GetCustomer(1);
    }
    else
        if (parseInt(data.d) === 2)
            alert('NRIC Number is exist in system');
        else
            alert('Insert customer fail');
}

function OnError() {
    alert("Heizz, bug roi");
}

function GetValTextbox(sControlName) {
    return $("input[id$='" + sControlName + "']").val();
}

function GetValDrop(sControlName) {
    var sRes = $("select[id$='" + sControlName + "'] option:selected").val();
    //alert(sControlName + sRes);
    return sRes;
}

function ValidateForm(bBranch, sFullName, sNRIC, txtDateBirth, sGender, sCity, sEducation) {
    
    if (bBranch === '') {
        alert("Branch name is require. Please choose one");
        return false;
    }
    else {
        
        if (sFullName === '') {
            alert("Full name is require. Please enter full name");
            return false;
        }
        else {
            
            if (sNRIC === '') {
                alert("NRIC Number is require. Please enter NRIC");
                return false;
            }
            else {
                
                if (txtDateBirth === '') {
                    alert("Date birth is require. Please enter Date birth");
                    return false;
                }
                else {
                    
                    if (sGender === '') {
                        alert("Gender is require. Please choose one");
                        return false;
                    }
                    else {
                        
                        if (sCity === '') {
                            alert("City is require. Please choose one");
                            return false;
                        }
                        else {
                            
                            if (sEducation === '') {
                                alert("Education is require. Please choose one");
                                return false;
                            }
                        }
                    }

                }
            }
        }
    }
    return true;
}

